<?php

// phpinfo();

require_once 'mp3\Smarty.php';
require_once 'mp3\GestionarPlaylist.php';
require_once 'mp3\GestionarCanco.php';

$smarty = new mp3_Smarty();
session_start();
require 'mp3/ComprovarSessio.php';
echo $smarty->fetch('mp3\header.tpl');


if (isset($_SESSION['userid'])){
	if (array_key_exists('gestionar', $_POST)){
		$playlists =Mp3_GestionarPlaylist::allPlaylists($_SESSION['userid']);
		$smarty->assign('playlists', $playlists);
		echo $smarty->fetch('mp3\gestionarPlaylists.tpl');
		echo $smarty->fetch('mp3\footer.tpl');
	}
	else if (array_key_exists('pl2', $_POST)){
		//Reproduir
		if (isset ($_GET['playlistid']) && $_GET['playlistid']>0){
			$_SESSION['playlistid'] = $_GET['playlistid'];
			$playlist = new Mp3_GestionarPlaylist($_GET['playlistid']);
			$playlist->loadXML();
			$cancons = $playlist->listCancons();
			$smarty->assign('cancons', $cancons);
			echo $smarty->fetch('mp3\eliminarCancons.tpl');
			echo $smarty->fetch('mp3\footer.tpl');
		}
	}
	else if (array_key_exists('eliminar', $_POST)){
		//Eliminar
		if (isset ($_GET['playlistid']) && $_GET['playlistid']>0){
			$_SESSION['playlistid'] = $_GET['playlistid'];
			$playlist = new Mp3_GestionarPlaylist($_GET['playlistid']);
			$playlist->delete();
			
			$smarty->assign('eliminar', 'ok');
			echo $smarty->fetch('mp3\index.tpl');
			echo $smarty->fetch('mp3\footer.tpl');
		}
	}
	else if (array_key_exists('canviEstat', $_POST)){
		if (isset ($_GET['id']) && $_GET['id']>0){
			$canco = new Mp3_GestionarCanco();
			$canco->cercarPerId($_GET['id']);
			$canco->publica=($canco->publica+1)%2;
			$canco->updateEstat();
			$smarty->assign('actualitzar', 'ok');
			echo $smarty->fetch('mp3\index.tpl');
			echo $smarty->fetch('mp3\footer.tpl');
		}
	}
	else if (array_key_exists('eliminarCanco', $_POST)){
		if (isset ($_GET['id']) && $_GET['id']>0){
			$playlist = new Mp3_GestionarPlaylist($_SESSION['playlistid']);
			$playlist->loadXML();
			$playlist->borrarCanco($_GET['id']);
			$playlist->saveXML();
			
			$smarty->assign('esborrar', 'ok');
			echo $smarty->fetch('mp3\index.tpl');
			echo $smarty->fetch('mp3\footer.tpl');
		}
	}
}
//	$smarty->assign('arxiu', $_POST['arxiu']);
//	$smarty->assign('canco', $canco);
//	echo $smarty->fetch('mp3\formulari.tpl');
